#import re
def writeLog(index, name, status, synonym ):
    file = "log.txt"
    logF = open(file, "a")
    text = "%d;%s;%s;%s\n" %(index, name, status, synonym)
    logF.write(text)
    logF.close()

def recordLastIndex(lastIndex):
    fn = "./lastIndex"
    fc = open(fn, "w")
    fc.write(str(lastIndex))
    fc.close()

def getStartIndex():
    fn = "./lastIndex"
    fc = open(fn, "r")
    startIndex = int(fc.read()) + 1
    fc.close()
    return startIndex

def getLastIndexFromLog():
    file = "./log.txt"
    logF = open(file, "r")
    rows = logF.readlines()
    if not any(rows) or rows == "\n":    # no record in the log file
        logF.close()
        return 1
    else:   # exist record in the log file, search the lastest row to get the index
        lastRow = rows[len(rows)-1]
        rowSep = lastRow.split(";") # format of per row is "[index],[name],[status]"
        lastIndex = int(rowSep[0])  # rowSep[0] is last index in previous execution, current is it plus 1
        logF.close()
        return lastIndex

def descJudge(desc):
    links = desc.findAll("a")
    status = links[0].get_text()
    if status == "accepted":
        return True
    pass

def readLog(file):
    logF = open(file, "r")
    rows = logF.read()
    list = {"kew_index": "", "formal_name": "", "status": "", "synonym": "" }
    for row in rows:
        field = row.split(";")
        list["kew_index"] = field[0]
        list["formal_name"] = field[1]
        list["status"] = field[2]
        list["synonym"] = field[3]
    pass